package main

import (
	_ "chess_platform/init"
	"chess_platform/internal/alisdk"
	"chess_platform/internal/common"
	_ "chess_platform/routers"
	"fmt"
	"github.com/astaxie/beego"
	"io/ioutil"
	"os"
)

func main() {
	if beego.BConfig.RunMode == "dev" {
		beego.BConfig.WebConfig.DirectoryIndex = true
		beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
	}
	beego.BConfig.WebConfig.Session.SessionOn = true
	beego.BConfig.RecoverPanic = true

	beego.SetStaticPath("/static", "conf/static")

	startHttp(common.PlatformConf.HttpPort, common.PlatformConf.AdminPort, common.PlatformId)
}

func startHttp(httpPort, adminPort int, platformId int64) {

	common.LogPc.Debug(fmt.Sprintf("httpPort:%v,adminPort:%v,platformId:%v", httpPort, adminPort, platformId))

	if httpPort != 0 && platformId != 0 {
		beego.BConfig.Listen.HTTPPort = httpPort
	}
	common.LogPc.Debug(fmt.Sprintf("Http Listening on:%v\n", beego.BConfig.Listen.HTTPPort))
	if adminPort != 0 && platformId != 0 {
		beego.BConfig.Listen.AdminPort = adminPort
	}
	common.LogPc.Debug(fmt.Sprintf("Admin Listening on:%v\n", beego.BConfig.Listen.AdminPort))

	pidFile := fmt.Sprintf("chess_platform_%v.pid", platformId)
	ioutil.WriteFile(pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0666)

	code := fmt.Sprintf("%v%v%v%v%v%v", platformId, platformId, platformId, platformId, platformId, platformId)
	if common.Dead {
		msg, err := alisdk.SendEmergencyText("18825165065", common.Sms.SmsSignName, common.Sms.SmsCode, code)
		if err != nil {
			common.LogPc.Error(fmt.Sprintf("服务器挂了发送短信通知失败:%v, err:%v", msg, err))
		}
	}
	/*
		beego.BConfig.Listen.EnableHTTPS = true
		beego.BConfig.Listen.HTTPSPort = 10443
		beego.BConfig.Listen.HTTPSKeyFile = "conf/ssl.key"
		beego.BConfig.Listen.HTTPSCertFile = "conf/ssl.crt"
	*/
	beego.Run()
}
